package cn.edu.zut.DAO;

import cn.edu.zut.model.Student;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class StudentDAO {
    private final static String DRIVER = "com.mysql.jdbc.Driver";
    private final static String DB_NAME = "student";
    private final static String URL = "jdbc:mysql://localhost:3306/" + DB_NAME;
    private final static String USER = "root";
    private final static String PASSWORD = "491015148";
    static Connection connection = null;
    static PreparedStatement statement = null;
    static ResultSet set = null;

    static {
        try {
            getConnection();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();

        // Class.forName(DRIVER);
        // return connection = DriverManager.getConnection(URL, USER, PASSWORD);
        return connection = dataSource.getConnection();
    }

    public static ResultSet DQL(String sql, Object... o) throws ClassNotFoundException, SQLException {

        statement = connection.prepareStatement(sql);
        for (int i = 0; i < o.length; i++) {
            statement.setObject((i + 1), o[i]);
        }
        set = statement.executeQuery();
        return set;
    }

    public static int DML(String sql, Object... o) throws ClassNotFoundException, SQLException {
        statement = connection.prepareStatement(sql);
        for (int i = 0; i < o.length; i++) {
            statement.setObject((i + 1), o[i]);
        }
        int executeUpdate = statement.executeUpdate();


        close();
        return executeUpdate;
    }

    public static void close() {
        closeSet(set);
        closeStatement(statement);
        closeCon(connection);
    }

    private static void closeSet(ResultSet set) {
        if (set != null) {
            try {
                set.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    private static void closeCon(Connection con) {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    private static void closeStatement(PreparedStatement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static <E> List<E> DaoDql(String sql, Class<E> cl, Object... objects) throws Exception {
        ResultSet dql = DQL(sql, objects);
        ResultSetMetaData data = dql.getMetaData();
        int columnCount = data.getColumnCount();
        List<E> list = new ArrayList<>();
        while (dql.next()) {
            E e = cl.newInstance();
            for (int i = 0; i < columnCount; i++) {
                Field field = cl.getDeclaredField(data.getColumnName(i + 1));
                field.setAccessible(true);
                field.set(e, dql.getObject(i + 1));
            }
            list.add(e);
        }
        close();
        return list;
    }

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        String sql = "select * from `user` where `username`=?";
        ResultSet dql = DQL(sql, "admin");
        ResultSetMetaData data = dql.getMetaData();
        int columnCount = data.getColumnCount();
        while (dql.next()) {
            for (int i = 0; i < columnCount; i++) {
                System.out.print(dql.getObject(i + 1) + "  ");
            }
            System.out.println();
        }

    }

    public static StudentDAO StudentDAOavgScore() {

    }

}